require.config({
    paths: {
        'jquery': ['http://cdn.bootcss.com/jquery/2.2.4/jquery.min', '/js/lib/jquery.min'],
        'doT': '/js/lib/doT.min',
        'bootstrap': '/js/lib/bootstrap.min',
        'datetimepicker': '/js/lib/bootstrap-datetimepicker.min',
        'select2': '/js/lib/select2.min',
        'paginator': '/js/lib/bootstrap-paginator'
    },
    shim: {
        'bootstrap': ['jquery'],
        'paginator': ['bootstrap']
    }
});

var myFun = {};
require(['jquery', 'doT', 'datetimepicker', 'select2', 'paginator', 'common', 'bootstrap'], function($, doT, datetimepicker, select2, paginator, common) {
    $(function() {
        var role = sessionStorage.getItem('role');
        var school_id = sessionStorage.getItem('school_id');
        var pagesize = 10;

        $('#order_info').hide();

        function initDatetimepicker() {
            var date = new Date();
            var date_string = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
            $('.order_date').datetimepicker({
                format: 'yyyy-mm-dd',
                autoclose: true,
                maxView: 'decade',
                minView: 'month',
                endDate: date
            });
            $('.order_time').datetimepicker({
                format: 'hh:ii:ss',
                autoclose: true,
                startView: 1,
                maxView: 'day',
                minView: 'hour',
                minuteStep: 1,
                title: date_string
            });

            $('#orderdateStart').val(date_string);
            $('#orderdateEnd').val(date_string);
            $('#timeStart').val('00:00:00');
            $('#timeEnd').val('23:59:59');
        }
        initDatetimepicker();


        //初始化学校
        function initSchool() {
            var data;
            $.get(common.urlBase + "/school/allSchool.json", function(data, status) {
                data = eval('(' + data + ')');
                var all = { "id": "-1", "name": "全部" };
                var schools = data.data;
                schools.unshift(all);
                var schoolTmpl = doT.template($('#template_school').text());
                $('#querySchool').html(schoolTmpl(schools));
                $("#querySchool").select2();
                if (role != 1) {
                    $('#querySchool').attr('disabled', true);
                    $('#querySchool').val(school_id);
                    $('#select2-querySchool-container').text($('#querySchool option[value='+school_id +']').text());
                }
            });
        }
        initSchool();

        //从数据库获取的数据替换订单信息

        $('#orderForm').submit(function() {
            var school = $('#querySchool').val();
            var start_time = Date.parse($('#orderdateStart').val() + ' ' + $('#timeStart').val());
            var end_time = Date.parse($('#orderdateEnd').val() + ' ' + $('#timeEnd').val());
            var status = $('#status').val();
            if (start_time >= end_time) {
                $('#tips').text('请选择正确的日期！');
                return false;
            } else {
                $('#tips').text('');
            }

            myFun.data = {
                'school_id': school,
                'start_time': start_time,
                'end_time': end_time,
                'status': status
            };

            refreshOrder(1);
            return false;

        });

        function getOrder(page, pagesize) {
            myFun.data.page = page;
            myFun.data.page_size = pagesize;
            $.post(common.urlBase + '/transaction/search/get', myFun.data, function(data, statu) {
                data = eval('(' + data + ')');
                if (data.ok) {
                    $('#order_info').show();
                    var orderTmpl = doT.template($('#template_order').text());
                    $('#order').html(orderTmpl(data.data.obj));
                    var total = data.data.total;
                    $('#total').text(total);
                    var pageNumber = Math.ceil(total / pagesize);
                    initPaginator(page, 8, pageNumber);

                } else {
                    $('#order_info').hide();
                    $('#noticeMsg').text(data['message']);
                    $('#noticeModal').modal('show');
                }

            });
            //将参数传给后台获得数据并初始化分页插件

        }

        function refreshOrder(page) {
            getOrder(page, pagesize);
        }

        function initPaginator(currentPage, numberOfPages, totalPages) {
            var options = {
                currentPage: currentPage,
                numberOfPages: numberOfPages,
                totalPages: totalPages,
                itemTexts: function(type, page, current) {
                    switch (type) {
                        case 'first':
                            return '首页';
                        case 'prev':
                            return '上一页';
                        case 'next':
                            return '下一页';
                        case 'last':
                            return '末页';
                        case 'page':
                            return page;

                    }
                },
                onPageChanged: function(event, oldPage, newPage) {
                    refreshOrder(newPage);
                }
            };
            $('#paginator').bootstrapPaginator(options);
        }

    });
});
